package com.hx.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import util.ToastUtil;

public class ToastActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mBtnToast1,mBtnToast2,mBtnToast3,mBtnToast4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
        mBtnToast1 = findViewById(R.id.btn_toast_1);
        mBtnToast2 = findViewById(R.id.btn_toast_2);
        mBtnToast3 = findViewById(R.id.btn_toast_3);
        mBtnToast4 = findViewById(R.id.btn_toast_4);
        mBtnToast1.setOnClickListener(this);
        mBtnToast2.setOnClickListener(this);
        mBtnToast3.setOnClickListener(this);
        mBtnToast4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_toast_1:
                Toast.makeText(getApplicationContext(),"Toast",Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_toast_2:
                Toast toastCenter = Toast.makeText(getApplicationContext(),"居中Toast",Toast.LENGTH_LONG);
                toastCenter.setGravity(Gravity.CENTER,0,0);
                toastCenter.show();
                break;
            case R.id.btn_toast_3:
                Toast toastCustom = new Toast(getApplicationContext());
                LayoutInflater Inflater = LayoutInflater.from(ToastActivity.this);
                View view = Inflater.inflate(R.layout.layout_toast,null);
                ImageView imageView = view.findViewById(R.id.tv_image);
                TextView textView = view.findViewById(R.id.tv_textview);
                imageView.setImageResource(R.drawable.icon_smile);
                textView.setText("自定义Toast");
                toastCustom.setView(view);
                toastCustom.setDuration(Toast.LENGTH_LONG);
                toastCustom.show();
                break;
            case R.id.btn_toast_4:
                ToastUtil.showMag(getApplicationContext(),"包装过的Toast");

                break;
        }

    }
}